package hn.cch.spring_boot_rest_quartz.runner;

import hn.cch.spring_boot_rest_quartz.entity.QuartzTrigger;
import hn.cch.spring_boot_rest_quartz.quartz.SpringQuartz;
import hn.cch.spring_boot_rest_quartz.service.QuartzService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;


@Component
public class QuartzRunner implements CommandLineRunner {

    public static Logger logger = LoggerFactory.getLogger(QuartzRunner.class);

    @Autowired
    private QuartzService quartzService;

    @Override
    public void run(String... args) throws Exception {

        for (int i = 0; i < 10; i++) {
            addJob(i);
        }

        List<QuartzTrigger> jobs = quartzService.getAllJob();
        logger.info("jobs size :" + jobs.size());

    }

    public void addJob(int index) {
        Class<? extends QuartzJobBean> jobClass = SpringQuartz.class;
        String jobName = "SpringJobBeanName" + index;
        String jobGroup = "SpringJobBeanGroup";
        String jobCorn = index + " " + index + " * * * ?";
        HashMap<String, String> jobData = new HashMap();
        jobData.put("key", "value");
        quartzService.scheduleJob(jobClass, jobName, jobGroup, jobCorn, jobData);
        logger.info(jobName + ":" + jobCorn);
    }
}
